﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ColemanSoftware.Delicious.Modules.Data
{
     class WebScrapper
    {

        public delegate string RequestAsync(string results);

        #region PRIVATE FIELDS

        private WebClient _client;
        private string _userName = "";
        private string _password = "";
       
        #endregion

        #region CONSTRUCTOR(S)

        public WebScrapper(UserAccount userAccount)
        {
            this._userName = userAccount.UserName;
            this._password = userAccount.Password;
            this._client = new WebClient();
        }

        public WebScrapper(string userName, string password)
        {
            this._userName = userName;
            this._password = password;
            this._client = new WebClient();
        }

        #endregion

        public string RequestData(string requestString)
        {
            _client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

            _client.Credentials = new NetworkCredential(_userName, _password);
            Stream resultStream = _client.OpenRead(requestString);
            StreamReader reader = new StreamReader(resultStream);
            string readerResult = reader.ReadToEnd();

            return readerResult;

        }


        public async Task<string> RequestDataAsync(string requestString, object returnData)
        {
            _client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

            _client.Credentials = new NetworkCredential(_userName, _password);

            string scrape = await _client.DownloadStringTaskAsync(new Uri(requestString));
            return scrape;
        }


    }
}
